import { loginApi, userApi } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

export default {
  namespaced: true, // 上锁
  state: {
    clientToken: '',
    token: getToken() || '',
    userInfo: JSON.parse(localStorage.getItem('user')) || {}
  },
  mutations: {
    clientFn(state, newClientToken) {
      state.clientToken = newClientToken
    },
    tokenFn(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    userFn(state, newUser) {
      state.userInfo = newUser
      localStorage.setItem('user', JSON.stringify(newUser))
    },
    // 删除
    removeFn(state) {
      state.token = ''
      removeToken()
      state.userInfo = ''
    }
  },
  actions: {
    async clientApi(store, data) {
      // 登录的接口
      const res = await loginApi(data)
      console.log(res)
      store.commit('clientFn', data.clientToken)
      store.commit('tokenFn', res.token)
      // 获取用户信息
      const req = await userApi(res.userId)
      console.log(req.data)
      store.commit('userFn', req.data)
      // console.log(res.data.token)
    }

  }
}

